package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.alibaba.fastjson.JSON;

import dao.AdminDao;
import entity.Admin;
import entity.Teacher;
import utils.DBManager;
import utils.JSONTools;

public class AdminDaoImpl implements AdminDao{

	@Override
	public boolean login(Admin admin) {
		String sql = "select * from admin where number = ? and password = ?";
		try {
			Connection conn = DBManager.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setLong(1, admin.getNumber());
			ps.setString(2, admin.getPassword());
			ResultSet rs = ps.executeQuery();
			if(rs.next())
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean doInsert(Admin admin) {
		String sql = "insert into admin(number,password,name) values (?,?,?)";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ps.setLong(1, admin.getNumber());
			ps.setString(2, admin.getPassword());
			ps.setString(3, admin.getName());
			if(ps.executeUpdate() == 1)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public JSON findAllTeacher() {
		String sql = "select number,name from teacher";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			JSON js = JSONTools.resultSetToJSON(rs);
			return js;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public JSON findAllAdmin() {
		String sql = "select number,name from admin";
		try {
			PreparedStatement ps = DBManager.getConnection().prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			JSON js = JSONTools.resultSetToJSON(rs);
			return js;
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean addTeacher() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update2Admin(Teacher teacher) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteTeacher(Teacher teacher) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
}
